//
//  PhotoNet.m
//  SportSocial
//
//  Created by yull on 14/12/13.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "PhotoNet.h"

@implementation PhotoNet

+ (AFHTTPRequestOperation *)photoPaginateWithUserId:(UInt64)userId
                                         PageNumber:(NSInteger)pageNumber
                                           pageSize:(NSInteger)pageSize
                                       successBlock:(httpSuccessBlock)successBlock
                                          failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user_photo/paginate",[self baseUrl]];
    
    NSDictionary *dict = @{@"token":[self token],
                           @"page_number":[NSNumber numberWithInt:pageNumber],
                           @"page_size":[NSNumber numberWithInt:pageSize],
                           @"user_id":[NSNumber numberWithLongLong:userId]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)queryByFromDate:(NSString *)fromDate
                                     toDate:(NSString *)toDate
                               successBlock:(httpSuccessBlock)successBlock
                                  failBlock:(httpFailBlock)failBlock
{
    NSNumber *user_id = [NSNumber numberWithLongLong:[CurrentUser shareInstance].userId];
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user_photo/query",[self baseUrl]];
    NSDictionary *dict = @{@"token":[self token],
                           @"user_id":user_id,
                           @"from_date":fromDate,
                           @"to_date":toDate};
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)uploadPhotoData:(NSData *)photoData
                               successBlock:(httpSuccessBlock)successBlock
                                  failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user_photo/upload?token=%@",[self baseUrl], [self token]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:photoData name:@"photo" fileName:@"photo.jpg" mimeType:@"multipart/form-data"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)deletePhotoWithUrlArray:(NSArray *)urlArray successBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSMutableString *url = [[NSMutableString alloc] initWithFormat:@"%@/user_photo/delete",[self baseUrl]];
    [url appendFormat:@"?token=%@",[self token]];
    
    for (NSInteger i = 0;i<urlArray.count;i++) {
        [url appendFormat:@"&file_url[%d]=%@",i,[urlArray objectAtIndex:i]];
    }
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}
@end
